package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupPosition;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.StarOverviewAdapter;
import flc.ast.bean.StarsBean;
import flc.ast.databinding.ActivityCharacterAnalysisBinding;
import flc.ast.dialog.SelStarDialog;
import stark.common.apis.ApiManager;
import stark.common.apis.base.ConstInfoBean;
import stark.common.base.IReqRetCallback;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;

public class CharacterAnalysisActivity extends BaseAc<ActivityCharacterAnalysisBinding> {

    public static String starsName;
    private List<StarsBean> listStar = new ArrayList<>();
    private BasePopupView selPopup;


    @Override
    protected int onCreate() {
        return R.layout.activity_character_analysis;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivCharacterAnalysisBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvStarName.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvStarName:
                selDialog();
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
        setData();
        getStarsAnalysis();
    }

    private void getData() {
        listStar.add(new StarsBean(getString(R.string.star_sign1), R.drawable.aimg_xz1_by, getString(R.string.birth_date1)));
        listStar.add(new StarsBean(getString(R.string.star_sign2), R.drawable.aimg_xz1_jn, getString(R.string.birth_date2)));
        listStar.add(new StarsBean(getString(R.string.star_sign3), R.drawable.aimg_xz1_sz2, getString(R.string.birth_date3)));
        listStar.add(new StarsBean(getString(R.string.star_sign4), R.drawable.aimg_xz1_jx, getString(R.string.birth_date4)));
        listStar.add(new StarsBean(getString(R.string.star_sign5), R.drawable.aimg_xz1_sz, getString(R.string.birth_date5)));
        listStar.add(new StarsBean(getString(R.string.star_sign6), R.drawable.aimg_xz1_cn, getString(R.string.birth_date6)));
        listStar.add(new StarsBean(getString(R.string.star_sign7), R.drawable.aimg_xz1_tc, getString(R.string.birth_date7)));
        listStar.add(new StarsBean(getString(R.string.star_sign8), R.drawable.aimg_xz1_tx, getString(R.string.birth_date8)));
        listStar.add(new StarsBean(getString(R.string.star_sign9), R.drawable.aimg_xz1_ss, getString(R.string.birth_date9)));
        listStar.add(new StarsBean(getString(R.string.star_sign10), R.drawable.aimg_xz1_mx, getString(R.string.birth_date10)));
        listStar.add(new StarsBean(getString(R.string.star_sign11), R.drawable.aimg_xz1_sp, getString(R.string.birth_date11)));
        listStar.add(new StarsBean(getString(R.string.star_sign12), R.drawable.aimg_xz1_sy, getString(R.string.birth_date12)));
    }

    //获取星座解析
    private void getStarsAnalysis() {
        if (!TextUtils.isEmpty(starsName)) {
            ApiManager.constApi().getConstInfo(this, starsName, new IReqRetCallback<ConstInfoBean>() {
                @Override
                public void onResult(boolean success, String msg, @Nullable ConstInfoBean result) {
                    if (success) {
                        setAnalysisData(result);
                    } else {
                        ToastUtils.showShort(getString(R.string.is_limit));
                    }
                }
            });
        }
    }


    //设置星座解析
    private void setAnalysisData(ConstInfoBean result) {
        mDataBinding.tvZzx.setText(result.getZgxx());
        mDataBinding.tvSx.setText(result.getSssx());
        mDataBinding.tvAnalysis.setText(result.getJttz());
    }

    private void selDialog() {
        SelStarDialog selDialog = new SelStarDialog(mContext);
//                polygonDialog.setCurrentPos(mSelectPolygonPos);
//                polygonDialog.show();
        selPopup = new XPopup.Builder(mContext).popupPosition(PopupPosition.Bottom) //手动指定弹窗的位置
                .isDestroyOnDismiss(false) //对于只使用一次的弹窗，推荐设置这个
                .atView(mDataBinding.tvStarName).hasShadowBg(false) // 去掉半透明背景
                .asCustom(selDialog);
        selPopup.show();
        selDialog.setListener(new SelStarDialog.Ilistener() {
            @Override
            public void onClick(String name) {
                starsName = name;
                mDataBinding.tvStarName.setText(starsName);
                getStarsAnalysis();
                setData();
            }
        });
    }

    private void setData() {
        for (StarsBean starsBean : listStar) {
            if (starsBean.getName().equals(starsName)) {
                mDataBinding.ivStarImg.setImageResource(starsBean.getImg());
                mDataBinding.tvStarBirthDate.setText(starsBean.getDate());
                mDataBinding.tvStarName.setText(starsBean.getName());
                break;
            }
        }
    }

}